<?php
/**
 * 页面静态化<p/>
 * 访问的页面实现静态化HTML，这样操作可以使得访问更快捷。
 * @cateproy 【页面静态化】
 * @author zhouhaichao(a)2008.sina.com
 * @version 1.0 & 2013-4-3
 * @copyright (c) 2013 
 */
class StaticPager
{
	var $cache;
	var $cacheFile;
	var $cacheFolder;
	public function __construct()
	{
		$request = Request::getInstance();
		if($request->mimeType && ".html" == $request->mimeType){
			$this->cache = TRUE;
			$this->cacheFile = $request->requestUrl;
			$this->cacheFolder = Config::getInstance()->getConfig("staticize",ROOT);
		}else{
			$this->cache = FALSE;
		}
	}
	
	public function startOb()
	{
		if($this->cache){
			@ob_start();
		}
	}

	public function endOb()
	{
		if($this->cache){
			$buffer = ob_get_contents();
			@ob_end_clean();
			echo $buffer;
			
			//生产静态文件.html
			if( $this->mkFileDir($this->cacheFolder.$this->cacheFile) ){
				file_put_contents($this->cacheFolder.$this->cacheFile,$buffer);
			}
		}
	}
	/**
	 * 创建文件
	 */
	function mkFileDir( $filename )
	{
		$dir = pathinfo($filename,PATHINFO_DIRNAME);
		return mkdirs($dir);
	}
}